// Space Sim 0.5.cpp : Defines the entry point for the console application.
/* Copyright (C) 2013  Reece Ingram

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"
#include <Windows.h>

//constants//
const int TOTALSYSTEMS=5;

#include "StarSystem.h"
#include "Player.h"

//globals//


StarSystem Start("Sol","This is your home system. In front of you is the the blue perl herself, planet Earth.","1",1);
StarSystem North("Proxima Centauri","The closest system to earth. Some believe it holds the key to unlocking the entire jumpgate network.","1",2);
StarSystem South("Alpha Centauri","The last 2 stars in the tripple star system, Centauri. Unimaginatively named A & B.","1",3);
StarSystem East("Wolf 359","A system filled with space junk bearing the mark of the ancient Earth federation...","1",4);
StarSystem West("Epsilon Eridani","One planet in this system is said to be the long-forgotten site of a human outpost, second only to earth in importance.","1",5);

StarSystem allSystems[TOTALSYSTEMS]= {Start, North, South, East, West};

int worldMap[TOTALSYSTEMS][TOTALSYSTEMS] = {
//    0 1 2 3 4      ^(y-axis)      ^(x-axis)
/*0*/{0,0,0,0,0},
/*1*/{0,0,2,0,0},
/*2*/{0,5,1,4,0},
/*3*/{0,0,3,0,0},
/*4*/{0,0,0,0,0} };

Player player(allSystems[1]);  // automate Player.currentCluster changes bassed on currentlocation, DONE - (dependent on TeleportPlayer();)


//function declarations   --   in definition order//
void ShowLicenceInfo(string fileToRead);
void GetWindowHeight(int* ptrWinHeightVar);
void GenerateWorld();
void Menu();

////////MAIN////////
int _tmain(int argc, _TCHAR* argv[])
{
	ShowLicenceInfo("gpl-3.0.txt");
	cout<<endl<<"Welcome! Type a command at the prompt to move or take another action. Type 'exit' or 'quit' to exit the game."<<endl;
	GenerateWorld();	
	//cout<<"World Generated"<<endl; //debug
	//player.GetSystemDescription();
	
	//cout<<player.currentLocation.adjSystems[1][0]<<endl;
	player.Teleport("Sol",2);
	Menu();

		
	cin.ignore();
	cout<<endl<<"Press ENTER to exit...";
	cin.get();
	return 0;
}

//function definitions    --    in declaration order//

void ShowLicenceInfo(string fileToRead) {     //to comply with code.google.com's rules
	cout<<"Space Sim 1  Copyright (C) 2013  Reece Ingram"<<endl<<endl<<"This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions."<<endl<<"Do you wish to see more info (Y/N)? ";
	char userChoice;
	cin>>userChoice;

	
	ifstream inFile(fileToRead);
	switch (userChoice) {
	case 'N':
	case 'n':		
		break;
	case 'Y':
	case 'y':
		string line;
		int lineNo =0;
		int winHeight =300;
		int* ptrWinHeight = &winHeight;
		cout<<endl;
		GetWindowHeight(ptrWinHeight);
		while (inFile.good()){           //while there are no errors or the file has not reached the end
			if(lineNo<=(winHeight-25)) {
				getline(inFile,line);
				cout<<line<<endl;
				lineNo++;
			} else {
				lineNo=0;
				cout<<"Press ENTER for Next Page...";
				cin.ignore();							//necessery to remove the previous ENTER from the "buffer"
				cin.get();
			}	
		}


		cout<<endl;
		break;
	}
	inFile.close();

}

void GetWindowHeight(int* ptrWinHeightVar) {
	CONSOLE_SCREEN_BUFFER_INFO currScreenBuffer;										//help from: http://msdn.microsoft.com/en-gb/library/windows/desktop/ms683171(v=vs.85).aspx
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &currScreenBuffer);     //           http://www.cplusplus.com/forum/windows/2761/#msg11466
	*ptrWinHeightVar = currScreenBuffer.dwSize.Y;										//           http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682093(v=vs.85).aspx
																						//		as well as Caroline's presentations for help with pointers :)
}

void GenerateWorld() {
	for (int i=0; i<TOTALSYSTEMS; i++) 
	{                              //iterates the worldMap y-axis
		for (int j=0; j<TOTALSYSTEMS; j++) 
		{                          //iterates the worldMap x-axis  --  "for each item in the worldMap"


			if (worldMap[i][j]!=0) 
			{                                  //ignore 0 value    --  "that isn't 0"
				//add adjacent systems to adjSystems[1][direction]
				for(int k=0;k<TOTALSYSTEMS;k++)
				{                      //iterate the allSystems array (for currently selected worldMap entity)
					if (allSystems[k].ID == worldMap[i][j]) 
					{


						//north adjSystems[1][0]
						if (worldMap[i-1][j] !=0) 
						{
							for (int l=0;l<TOTALSYSTEMS;l++) 
							{
								if (allSystems[l].ID==worldMap[i-1][j]) 
								{ 
									allSystems[k].adjSystems[1][0]=allSystems[l].name;
									//<<"TESTESTESTESTESTESTEST"<<endl;
									allSystems[k].adjSystemsDispList.push_back("<"+allSystems[l].name+">"+" is north of here. ");//generate System's surrounding's sentences (for the adjSystemsDispList vector)
								}
							}
						}
						//south adjSystems[1][1]
						if (worldMap[i+1][j] !=0) 
						{
							for (int l=0;l<TOTALSYSTEMS;l++) 
							{
								if (allSystems[l].ID==worldMap[i+1][j]) 
								{ 
									allSystems[k].adjSystems[1][1]=allSystems[l].name;
									allSystems[k].adjSystemsDispList.push_back("<"+allSystems[l].name+">"+" is south of here. ");//generate System's surrounding's sentences (for the adjSystemsDispList vector)
									
								}
							}
						}
						//east adjSystems[1][2]
						if (worldMap[i][j+1] !=0) 
						{
							for (int l=0;l<TOTALSYSTEMS;l++) 
							{
								if (allSystems[l].ID==worldMap[i][j+1]) 
								{ 
									allSystems[k].adjSystems[1][2]=allSystems[l].name;
									allSystems[k].adjSystemsDispList.push_back("<"+allSystems[l].name+">"+" is east of here. ");//generate System's surrounding's sentences (for the adjSystemsDispList vector)
								}
							}
						}
						//west adjSystems[1][3]
						if (worldMap[i][j-1] !=0) 
						{
							for (int l=0;l<TOTALSYSTEMS;l++) 
							{
								if (allSystems[l].ID==worldMap[i][j-1]) 
								{ 
									allSystems[k].adjSystems[1][3]=allSystems[l].name;
									allSystems[k].adjSystemsDispList.push_back("<"+allSystems[l].name+">"+" is west of here. "); //generate System's surrounding's sentences (for the adjSystemsDispList vector)
								}
							}
						}


					}
				}
			}


		}
	}

}

void Menu() 
{
	string userChoice;
	bool loopMenu = true;
	while (loopMenu) 
	{
		cout<<endl<<"\\\\\\MENU///"<<" --- Turn: "<<player.turns<<endl<<"The directions you can move in and any other options will appear below."<<endl<<"KEY: n = North  s = South  e = East  w = West"<<endl<<endl;
		
		//print directions
		cout<<"Type This"<<" --- "<<"To go here"<<endl<<"------------------------"<<endl;
		for (int i=0; i<TOTALDIRECTIONS; i++)    //itterate the adjSystems array
		{
			if (player.currentLocation.adjSystems[1][i] != "0")
			{
				string direcLetter=player.currentLocation.adjSystems[0][i];
				string direcDesc;
				for (int j=0; j<TOTALSYSTEMS;j++) 
				{
					if (allSystems[j].name==player.currentLocation.adjSystems[1][i])
					{
						direcDesc = player.currentLocation.adjSystems[1][i] + " System";
					}
				}
				cout<<direcLetter<<" --- "<<direcDesc<<endl;
			}
			
		}

		cout<<endl; //seperates  directions  from  extra options
		//TODO: Add extra options section
		cout<<"Type this"<<" --- "<<"To do this"<<endl<<"------------------------"<<endl;
		cout<<"exit"<<" --- "<<"Quit the game"<<endl;
		//get user choice and act on it
		cout<<endl<<">>> ";
		cin>>userChoice;
		for (int i=0; i<TOTALDIRECTIONS;i++)
		{
			if (userChoice==player.currentLocation.adjSystems[0][i] && player.currentLocation.adjSystems[1][i] != "0")
			{
				//cout<<" Choice Valid"<<endl;  //Debug
				player.Move(player.currentLocation.adjSystems[1][i]);
			} 
			else if (userChoice=="exit"||userChoice=="quit") //could use pointer-to-member-function to improve this by having an array of them in the StarSystem class
			{
				loopMenu=false;
			}
			else if (userChoice!="n" && userChoice!="s" && userChoice!="e" && userChoice!="w")//else if - to add extra, non-direction, options
			{
				cout<<">>> Choice Invalid! Try Again..."<<endl;
			} 
			

		}
		player.turns++;
	}
}